package com.rjt.taobao.controller;

import blade.kit.DateKit;
import blade.kit.http.HttpRequest;
import com.rjt.common.util.CookieUtil;
import com.rjt.common.util.DateUtil;
import com.rjt.common.util.JsonUtil;
import com.rjt.common.util.MD5Util;
import com.rjt.common.util.TextUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**  

* <p>Title: AppCtroller.java</p>  

* <p>Description: </p>  

* @author renjiangtao 

* @date 2018年6月25日  

* @version 1.0  

*/
@Controller
@RequestMapping("tb")
public class TbController {
	
	private Logger logger = LoggerFactory.getLogger(TbController.class);
	
	private String imgUrl ;
			
	private String lgToken;
	
	private  String cookie;
	
	private String taobaoName;
	
	private String returnUrl;
	
	private String umidToken="C"+DateKit.getCurrentUnixTime()+TextUtil.getRandomNum(14)
	+DateKit.getCurrentUnixTime()+TextUtil.getRandomNum(6);
	
	@GetMapping(value = "/index")
	public String testPage(HttpServletRequest request, HttpServletResponse response) {
		logger.warn("ip:{} 淘宝活动",TextUtil.getRemortIP(request));
		return "taobao";
	}
	
	private  String generateQRCodeInit() {
		
		String url="https://qrlogin.taobao.com/qrcodelogin/generateQRCode4Login.do?adUrl=&adImage="
				+ "&adText=&viewFd4PC=&viewFd4Mobile=&from=tb&appkey=00000000&umid_token="+umidToken+
				"&_ksTS="+DateUtil.getCurrent13Time()+"_"+TextUtil.getRandomNum(2)+"&callback=jsonp"+
				TextUtil.getRandomNum(2);
        HttpRequest request = HttpRequest.get(url);
        String res = request.body();
        res=res.substring(20, res.length()-7);
        cookie = CookieUtil.getCookie(request);
		request.disconnect();
        return res;
	}
	@GetMapping("generateQRCode4Login")
	@ResponseBody
	public String generateQRCode4Login(HttpServletResponse response) throws Exception {
        String result = generateQRCodeInit();
        if(JsonUtil.getJsonValue(result, "success").equals("true")) {
        	 imgUrl=JsonUtil.getJsonValue(result, "url");
             lgToken=JsonUtil.getJsonValue(result, "lgToken");
             System.out.println(imgUrl);
             response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
 	         response.setHeader("Pragma", "No-cache");//设置响应头信息，告诉浏览器不要缓存此内容
 	         response.setHeader("Cache-Control", "no-cache");
 	         response.setDateHeader("Expire", 0);
 	         OutputStream os = response.getOutputStream();
 	         String url="https:"+imgUrl;
 	         System.out.println(url);
 	         HttpRequest request = HttpRequest.get(url).header("Cookie", this.cookie);
 	         os.write(request.bytes());
 			 request.disconnect();
 	         os.flush();
 	         os.close();  
 	         return "success";
        }else {
        	return "error";
        }
	}
	
	@GetMapping("qrcodeLoginCheck")
	@ResponseBody
	public String qrcodeLoginCheck()  {
		String url="https://qrlogin.taobao.com/qrcodelogin/qrcodeLoginCheck.do?lgToken="+
				lgToken+"&defaulturl=https%3A%2F%2Fwww.taobao.com%2F&_ksTS="+DateUtil.getCurrent13Time()
				+"_"+TextUtil.getRandomNum(2)+"&callback=jsonp"+TextUtil.getRandomNum(2);
         System.out.println(url);
         HttpRequest request = HttpRequest.get(url).header("Cookie", this.cookie);
         String res = request.body();
 		 request.disconnect();
         return res;
	}
	@PostMapping("getName")
	@ResponseBody
	public String getName(String url) throws UnsupportedEncodingException  {
         System.out.println(url);
         String name=TextUtil.getSubString(url, "cntaobao", "&");
         taobaoName = URLDecoder.decode(name, "UTF8");
         System.err.println(taobaoName);
         returnUrl=url;
         return taobaoName;
	}
	
	@GetMapping("getHeadImage")
	@ResponseBody
	public String getHeadImage(HttpServletResponse response) throws IOException   {
         String url="https://wwc.alicdn.com/avatar/getAvatar.do?userNick="+taobaoName+"&width=160&height=160&type=sns&_input_charset=UTF-8";
         HttpRequest request = HttpRequest.get(url).header("Cookie", this.cookie);
         response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
         response.setHeader("Pragma", "No-cache");//设置响应头信息，告诉浏览器不要缓存此内容
         response.setHeader("Cache-Control", "no-cache");
         response.setDateHeader("Expire", 0);
         OutputStream os = response.getOutputStream();
         os.write(request.bytes());
		 request.disconnect();
         os.flush();
         os.close();  
         return "success";
	}
	
	@GetMapping("getTqValue")
	@ResponseBody
	public String getTqValue() throws IOException   {
         String url=returnUrl+"&umid_token="+umidToken;
         HttpRequest request = HttpRequest.get(url).header("Cookie", this.cookie).followRedirects(false);
         cookie = CookieUtil.getCookie(request);
         request.disconnect();
         String urlNew="https://vip.taobao.com/ajax/getGoldUser.do?_input_charset=utf-8&from=diaoding&_ksTS="+DateUtil.getCurrent13Time()
         +"_"+TextUtil.getRandomNum(2)+"&callback=jsonp"+TextUtil.getRandomNum(3);
         HttpRequest requests = HttpRequest.get(urlNew).header("Cookie", this.cookie).header("Referer","http://vip.taobao.com");
         String res=requests.body();
         res=res.substring(9, res.length()-2);
         requests.disconnect();
         return res;
	}
	@GetMapping("getBuyInfo")
	@ResponseBody
	public String getBuyInfo() throws IOException   {
         String url="https://h5api.m.taobao.com/h5/mtop.taobao.mclaren.getmytaobaopage/1.0/?jsv=2.4.0&api=mtop.taobao.mclaren.getMyTaobaoPage&v=1.0&ecode=1&type=jsonp&dataType=jsonp&callback=mtopjsonp1&data=%7B%22pageName%22%3A%22index%22%7D";
         HttpRequest request = HttpRequest.get(url).header("Cookie", this.cookie).followRedirects(false);
         cookie = CookieUtil.getCookie(request);
         System.out.println(cookie);
         request.disconnect();
         String token=TextUtil.getSubString(cookie, "_m_h5_tk", ";");
         token=token.substring(0, token.indexOf("_"));
         System.out.println(token);
         String time=DateUtil.getCurrent13Time();
         String sign=MD5Util.getMD5(token+"&"+time+"&12574478&{\"pageName\":\"index\"}");
         System.out.println(sign);
         
         String urlNew="https://h5api.m.taobao.com/h5/mtop.taobao.mclaren.getmytaobaopage/1.0/?jsv=2.4.0&appKey=12574478&t="+time+"&sign="+sign
         +"&api=mtop.taobao.mclaren.getMyTaobaoPage&v=1.0&ecode=1&type=jsonp&dataType=jsonp&callback=mtopjsonp2&data=%7B%22pageName%22%3A%22index%22%7D";		 
         HttpRequest requests = HttpRequest.get(urlNew).header("Cookie", this.cookie);
         String res=requests.body();
         System.out.println(res);
         requests.disconnect();
         return res;
	}
	
	@GetMapping("getCardInfo")
	@ResponseBody
	public String getCardInfo() throws IOException   {
         String url="https://h5api.m.taobao.com/h5/mtop.taobao.mclaren.getmytaobaopage/1.0/?jsv=2.4.0&api=mtop.taobao.mclaren.getMyTaobaoPage&v=1.0&ecode=1&type=jsonp&dataType=jsonp&callback=mtopjsonp1&data=%7B%22pageName%22%3A%22index%22%7D";
         HttpRequest request = HttpRequest.get(url).header("Cookie", this.cookie).followRedirects(false);
         cookie = CookieUtil.getCookie(request);
         System.out.println(cookie);
         request.disconnect();
         String token=TextUtil.getSubString(cookie, "_m_h5_tk", ";");
         token=token.substring(0, token.indexOf("_"));
         System.out.println(token);
         String time=DateUtil.getCurrent13Time();
         String sign=MD5Util.getMD5(token+"&"+time+"&12574478&{\"ids\":\"{\\\"dimensions\\\":[3,0,1,2]}\",\"from\":\"\"}");
         System.out.println(sign);
         String urlNew="https://api.m.taobao.com/h5/mtop.taobao.alistar.dimensions.getdata/1.0/?jsv=2.4.2&appKey=12574478&t="+time+"&sign="+sign
         +"&api=mtop.taobao.alistar.dimensions.getData&v=1.0&type=jsonp&dataType=jsonp&timeout=20000&callback=mtopjsonp1&data=%7B%22ids%22%3A%22%7B"
         + "%5C%22dimensions%5C%22%3A%5B3%2C0%2C1%2C2%5D%7D%22%2C%22from%22%3A%22%22%7D";		 
         HttpRequest requests = HttpRequest.get(urlNew).header("Cookie", this.cookie);
         String res=requests.body();
         System.out.println(res);
         urlNew="https://healthcenter.taobao.com/home/json/GetPunishHistory.json?page=1&startTime=&endTime=&pointRange=0%2C1%2C2%2C5&"
         		+ "_ksTS=1529307112639_52&callback=jsonp53";		 
          requests = HttpRequest.get(urlNew).header("Cookie", this.cookie);
          res=requests.body();
          System.out.println(res);
         requests.disconnect();
         return res;
	}
	@GetMapping("getSexInfo")
	@ResponseBody
	public String getSexInfo() throws IOException   {
         String url="https://member1.taobao.com/member/fresh/account_profile.htm";
         HttpRequest request = HttpRequest.get(url).header("Cookie", this.cookie);
         System.out.println(cookie);
         String res=request.body();
         request.disconnect();
         System.out.println(res);
         return res;
	}
	
	public static void main(String[] args) {
		String urlNew="https://vip.taobao.com/ajax/getGoldUser.do?_input_charset=utf-8&from=diaoding&_ksTS=1533176269221_26&callback=jsonp472" ;
		String cookies="mt=np=; Domain=.taobao.com; Expires=Thu, 09-Aug-2018 02:17:59 GMT; Path=/;tg=0; Domain=.taobao.com; Expires=Sat, 18-Apr-2020 02:17:59 GMT; Path=/;cookie17=UU2zUzb21u3YEw%3D%3D;Domain=.taobao.com;Path=/;HttpOnly;_nk_=%5Cu594B%5Cu6597%5Cu7684%5Cu5766%5Cu7136; Domain=.taobao.com; Path=/;dnk=%5Cu594B%5Cu6597%5Cu7684%5Cu5766%5Cu7136; Domain=.taobao.com; Path=/;_cc_=W5iHLLyFfA%3D%3D; Domain=.taobao.com; Expires=Fri, 02-Aug-2019 02:17:59 GMT; Path=/;lgc=%5Cu594B%5Cu6597%5Cu7684%5Cu5766%5Cu7136; Domain=.taobao.com; Expires=Sat, 01-Sep-2018 02:17:59 GMT; Path=/;tracknick=%5Cu594B%5Cu6597%5Cu7684%5Cu5766%5Cu7136; Domain=.taobao.com; Expires=Fri, 02-Aug-2019 02:17:59 GMT; Path=/;existShop=MTUzMzE3NjI3OQ%3D%3D; Domain=.taobao.com; Path=/;tbcp=f=UUjZelQNcVltrRPcCO5SnGD1ugQ%3D&e=UNJQ5LiIIPuzFXjzZ7ZqNg%3D%3D; Domain=.caipiao.taobao.com; Path=/;uc3=vt3=F8dBzrmTcFnXQY9D3DU%3D&id2=UU2zUzb21u3YEw%3D%3D&nk2=1BvTvaNDy3UAgQ%3D%3D&lg2=VT5L2FSpMGV7TQ%3D%3D;Domain=.taobao.com;Path=/;Expires=Sat, 01-Sep-2018 02:17:59 GMT;HttpOnly;csg=50e0ac66; Domain=.taobao.com; Path=/;cookie1=Vve%2F8wAPAyz1trXBHHq4wRSZDRY07l1bbPWFTraEw64%3D;Domain=.taobao.com;Path=/;HttpOnly;cookie2=10fbe3dae0aa742be848d072e8783291;Domain=.taobao.com;Path=/;HttpOnly;log=lty=UQ%3D%3D;Domain=login.taobao.com;Path=/;Expires=Sat, 01-Sep-2018 02:17:59 GMT;HttpOnly;lc=VynM0RdusmeMPO0AWqjS;Domain=login.taobao.com;Path=/;Expires=Sat, 01-Sep-2018 02:17:59 GMT;HttpOnly;skt=832de299208c60d7;Domain=.taobao.com;Path=/;HttpOnly;_l_g_=Ug%3D%3D; Domain=.taobao.com; Path=/;t=53ecaa263ee581dc74c1723f546648ff; Domain=.taobao.com; Expires=Wed, 31-Oct-2018 02:17:59 GMT; Path=/;sg=%E7%84%B651; Domain=.taobao.com; Path=/;uc1=cookie16=W5iHLLyFPlMGbLDwA%2BdvAGZqLg%3D%3D&cookie21=UIHiLt3xThH8t7YQoFNq&cookie15=W5iHLLyFOGW7aA%3D%3D&existShop=false&pas=0&cookie14=UoTfKLc77%2FCgSQ%3D%3D&tag=8&lng=zh_CN; Domain=.taobao.com; Path=/;lid=%E5%A5%8B%E6%96%97%E7%9A%84%E5%9D%A6%E7%84%B6; Domain=login.taobao.com; Expires=Fri, 02-Aug-2019 02:17:59 GMT; Path=/;unb=2513880155;Domain=.taobao.com;Path=/;HttpOnly;" ;
		HttpRequest requests = HttpRequest.get(urlNew).header("Cookie", cookies).header("Referer","http://vip.taobao.com");
        String res=requests.body();
		System.out.println(res);
	}
}
